using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_PetSlotCell : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_PetSlotCell
        {
            #region ObjectBinding Generate 
            public UnityEngine.GameObject stateLockLv { protected set; get; }
            public UnityEngine.GameObject stateLockCost { protected set; get; }
            public UnityEngine.GameObject stateUsing { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("stateLockLv", out var __tbv0);
                this.stateLockLv = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("stateLockCost", out var __tbv1);
                this.stateLockCost = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("stateUsing", out var __tbv2);
                this.stateUsing = __tbv2;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields

        private UIPetSlotType slotType = UIPetSlotType.Unlock;
        private int currIndex = 0;
        private System.Action<UIPetSlotType, int> callOnClick = null;

        #endregion fields

        #region properties

        protected UIB_UIN_PetSlotCell ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_PetSlotCell();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            var btnEx = this.gameObject.GetOrAdd<ExButton>();
            btnEx.onClick.AddListener(this.OnClickSelf);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds

        public void Show(UIPetSlotType slotType, int index, System.Action<UIPetSlotType, int> funcCall)
        {
            this.SetData(slotType, index, funcCall);
            this.Show();
            this.ui.stateLockLv.SetActive(slotType == UIPetSlotType.LockByLv);
            this.ui.stateLockCost.SetActive(slotType == UIPetSlotType.LockByItem);
            this.ui.stateUsing.SetActive(slotType == UIPetSlotType.Used);
        }

        private void SetData(UIPetSlotType slotType, int index, System.Action<UIPetSlotType, int> funcCall)
        {
            this.slotType = slotType;
            this.currIndex = index;
            this.callOnClick = funcCall;
        }

        private bool IsUnlock()
        {
            return this.slotType == UIPetSlotType.Used || this.slotType == UIPetSlotType.Unlock;
        }

        private void OnClickSelf()
        {
            if (this.IsUnlock())
                return;
            this.callOnClick?.Invoke(this.slotType, this.currIndex);
        }
        #endregion methonds
    }
}
